package com.zhongge.sort;

import java.util.Arrays;

/**
 * @ClassName Bubble
 * @Description TODO 冒泡排序
 * @Author zhongge
 * @Date 2025/9/10 14:46
 * @Version 1.0
 */
public class Bubble {
    public static void bubbleSort(int[] array) {
        //外层控制趟数
        for (int i = 0; i < array.length - 1; i++) {
            //优化
            boolean flag = false;//如果基本有序就为false
            //内层控制比较次数
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    //一旦你比较过 那么你就不是基本有序地
                    flag = true;
                }
            }
            //判断
            if (!flag) {
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {5,4,3,2,1,10,9,8,7,6};
        System.out.println("排序前：" + Arrays.toString(array));
        bubbleSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }
}
